//
//  HEApplyCirlceSquareViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/2.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEApplyCirlceSquareViewController.h"
#import "UICollectionView+LTCollectionViewLayoutCell.h"
#import "HERoomApplyCell.h"
#import "HERoomApplyModel.h"
#import "HEApplyCirlceViewController.h"

static NSString * const reuseIdentitier = @"HERoomApplyCell";

typedef NSMutableArray <NSMutableArray<NSNumber *> *> IndexCountBySection;

@interface HEApplyCirlceSquareViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, LTCollectionViewDynamicHeightCellLayoutDelegate, HERoomApplyCellDelegate>


@property (nonatomic, copy) NSArray *photoEntityFromWallJSONData;
@property (nonatomic, strong) NSMutableArray  <HERoomApplyModel *> *dataSource;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) IndexCountBySection *indexCountBySectionForHeight;
@property (strong, nonatomic) LTCollectionViewDynamicHeightCellLayout *customizeLayout;

@property (nonatomic) NSInteger cellColumn;
@property (nonatomic) CGFloat cellMargin;

@end

@implementation HEApplyCirlceSquareViewController
   
#pragma mark -

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    self.title = @"申请广场";
     
    [self initUI];
    [self buildData];
    [self loadData];
}

- (void)refresh {
    [self.collectionView.lt_indexPathHeightCache invalidateAllHeightCache];
    [self collectionView:self.collectionView numberOfItemsInSection:0];
    [self.collectionView reloadData];
}

- (void)initUI {
    self.view.backgroundColor = CViewBgColor;
    
    QMUIFillButton *specBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    specBtn.titleLabel.font = DYBlodFont(17);
    [specBtn setTitle:@"我要申请" forState:UIControlStateNormal];
    [self.view addSubview: specBtn];
    [specBtn addTarget:self action:@selector(popSpecViewClick) forControlEvents:UIControlEventTouchUpInside];

    
    // 1.创建流水布局
    _customizeLayout = [[LTCollectionViewDynamicHeightCellLayout alloc] init];
    _customizeLayout.dynamicLayoutDelegate = self;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_customizeLayout];
    _collectionView.backgroundColor = CViewBgColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    _collectionView.mj_header = header;
    
    [specBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(ZXNavBarHeight + 20);
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.height.offset(40);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(specBtn.mas_bottom).offset(20);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    [self.collectionView registerClass:[HERoomApplyCell class] forCellWithReuseIdentifier:@"HERoomApplyCell"];
}

- (void)loadData {
    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KRoomApplys parameters:@{} success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        self.dataSource = [NSMutableArray arrayWithArray:[HERoomApplyModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self refresh];
        [self.collectionView.mj_header endRefreshing];

    } failure:^(NSError * _Nonnull error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}

- (void)buildData {
    
    _indexCountBySectionForHeight = [NSMutableArray array];
    _cellColumn = 2;
    _cellMargin = 0;
    
    NSMutableArray *entities = @[].mutableCopy;
    self.photoEntityFromWallJSONData = entities;
}

#pragma mark -
- (void)popSpecViewClick {
    HEApplyCirlceViewController *vc = [HEApplyCirlceViewController new];
    vc.backBlock = ^{
        [self.collectionView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - LTCollectionViewDynamicHeightCellLayoutDelegate
- (NSInteger) numberOfColumnWithCollectionView:(UICollectionView *)collectionView
                          collectionViewLayout:( LTCollectionViewDynamicHeightCellLayout *)collectionViewLayout{
    return _cellColumn;
}

- (CGFloat) marginOfCellWithCollectionView:(UICollectionView *)collectionView
                     collectionViewLayout:(LTCollectionViewDynamicHeightCellLayout *)collectionViewLayout{
    return _cellMargin;
}


- (NSMutableArray<NSMutableArray *> *)indexHeightOfCellWithCollectionView:(UICollectionView *)collectionView collectionViewLayout:(LTCollectionViewDynamicHeightCellLayout *)collectionViewLayout {
    return _indexCountBySectionForHeight.mutableCopy;
}


#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSMutableArray<NSNumber *> *indexHeightArray = @[].mutableCopy;
    for (NSInteger i = 0; i < self.dataSource.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:section];
        CGFloat height= [collectionView lt_heightForCellWithIdentifier:reuseIdentitier fixedWidth:KScreenWidth cacheByIndexPath:indexPath configuration:^(HERoomApplyCell *cell) {
            cell.lt_enforceFrameLayout = NO;
            cell.model = self.dataSource[indexPath.row];
        }];
        
        [indexHeightArray addObject:@(height)];
    }
    _indexCountBySectionForHeight[section] = indexHeightArray;
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HERoomApplyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentitier forIndexPath:indexPath];
    cell.lt_enforceFrameLayout = NO;
    cell.model = self.dataSource[indexPath.row];
    cell.delegate = self;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - HERoomApplyCellDelegate
- (void)helpApplyRoomWithCell:(HERoomApplyModel *)model {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KRoomSaveApply parameters:model success:^(id  _Nonnull responseObject) {
        HEObjectDataResponse *response = [HEObjectDataResponse mj_objectWithKeyValues:responseObject];
        
        if (response.success) {
            if (response.data.integerValue == model.cnt) {
                [Dialog toastBottom:@"您已助力过了！"];
            }else {
                model.KnumText = nil;
                model.cnt = response.data.integerValue;
                [Dialog toastBottom:@"助力成功"];
                [self refresh];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

@end
